package com.eipc;

import org.androidpn.sdk.Constants;
import org.androidpn.sdk.XmppManager;

import com.eipc.model.Player;
import com.eipc.model.PlayerGroup;
import com.eipc.model.SerInfoResponse;
import com.eipc.util.DBHelper;
import com.eipc.util.HttpManager;
import com.eipc.util.MyLog;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * 选手档案
 * @author x_liaolijun
 *
 * 2010-9-12 上午08:46:12
 *  
 * PlayerRecordActivity
 *
 */
public class PlayerRecordActivity extends CommonActivity {
	public static int TYPE_SINGLE = 1; //单打
	public static int TYPE_DOUBLE = 2; //双打
	public boolean isNotRef = true;
	private ListView mListView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.player_record);
		initView();
	}
	
	@Override
	protected void initView() {
		super.initView();
		mBar.setMiddleText(R.string.player_record); 
		mBar.setOnRightClickListener(new View.OnClickListener() {		
			@Override
			public void onClick(View v) {
				if(isNotRef){
					new LoadPlayersTask().execute();
				}
			}
		});
		mListView = (ListView)findViewById(R.id.list1);
		mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_1, android.R.id.text1, mRe.getStringArray(R.array.player_recode_single_double)));
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				Intent i = new Intent(PlayerRecordActivity.this, PlayerRecordActivity2.class);
				if(position==0){//单打
					i.putExtra("type", TYPE_SINGLE); 
				}else{
					i.putExtra("type", TYPE_DOUBLE); 
				}
				startActivityForResult(i,REQUEST_CODE_CHILD);
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == REQUEST_CODE_CHILD && resultCode == RESULT_CANCELED){
			finish();
		}
	}
	
	class LoadPlayersTask extends CommonTask<Void>{
		private SerInfoResponse playerSir;
		private SerInfoResponse playerGroupSir;
		private boolean isOk = false;
		
		public LoadPlayersTask(){
			super(PlayerRecordActivity.this);
		}
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			//TODO 删除数据
			mDB.deleteAll(DBHelper.TB_MATCH);
			mDB.deleteAll(DBHelper.TB_SCORE);
		}
		
		@Override
		protected Void doInBackground(Void... params) {
			isNotRef = false;
			Log.i("--"+this.getClass().getName(),"------------------PollingMatchScoreTask------------------ right : ");
			try {
				SharedPreferences sdkPreferences = getSharedPreferences(Constants.SDK_PREFERENCES, Context.MODE_PRIVATE);
				String mobileID = sdkPreferences.getString(Constants.XMPP_USERNAME,"");
				playerSir = HttpManager.loadSerInfo(getString(R.string.url_load_serinfo),mobileID,mDB.GetPlayerSql(),Constants.LOAD_MODEL_PLAYER);
				playerGroupSir = HttpManager.loadSerInfo(getString(R.string.url_load_serinfo),mobileID,mDB.GetPlayerGroupSql(),Constants.LOAD_MODEL_PLAYERGROUP);
				if(playerSir!=null&&playerGroupSir!=null){
					//TODO 刷新成功 等待 PUSH	
					try{mDB.savePlayers((Player[])playerSir.os);}catch(Exception e){mBuilder.setMessage(R.string.status_save_player_failed).setNegativeButton(R.string.btn_close, null).show();}
					try{mDB.savePlayerGroups((PlayerGroup[])playerGroupSir.os);}catch(Exception e){mBuilder.setMessage(R.string.status_save_player_group_failed).setNegativeButton(R.string.btn_close, null).show();}
					isOk = true;
				}
			} catch (Exception e) {
				MyLog.error(FetchConsultService.class,"",e);
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			isNotRef = true;
			if(!isOk){
				mBuilder.setMessage(R.string.status_connect_failed).setNegativeButton(R.string.btn_close, null).show();
				Log.e(this.getClass().getName()+" polling failure : ","mResult is null");
			}
		}
		
	}
}
